function Player(/*Vec2*/ position, /*MapManager*/ mapManager){
	this.lastPosition = vec2(-1,-1);
	this.position = position;
	this.mapManager = mapManager;
	this.direction = 0;
	this.action = false;
}

Player.prototype.act = function(){
	this.action = true;
};

Player.prototype.moveFacing = function(){
	var dir = this.mapManager.getDirectionVector(this.direction);
	
	var lp = this.position.clone();
	this.position.sum(dir);
	
	var solid = this.mapManager.isSolid(this.position);
	if (solid == 1){
		this.position.sum(-dir.a,-dir.b);
		return;
	}else if (solid == 2){
		this.position.sum(dir);
	}
	
	var ins = this.mapManager.getInstancesAt(this.position);
	for (var i=0,len=ins.length;i<len;i++){
		if (ins[i].solid){
			var s = (solid == 2)? 2 : 1;
			this.position.sum(-dir.a * s,-dir.b * s);
			return;
		}
	}
	
	this.lastPosition = lp;	
	this.act();
};

Player.prototype.look = function(){
	var obj = this.mapManager.castRay(this.position, this.direction);
	if (obj.length > 0){
		for (var i=0,len=obj.length;i<len;i++){
			var msg = "You see ";
			
			var l = obj[i][0].toLowerCase();
			msg += (l == "a" || l == "e" || l == "i" || l == "o" || l == "u")? "an" : "a";
			
			msg += " " + obj[i];
			Console.addMessage(msg, "see" + obj[i], "white");
		}
	}else{
		Console.addMessage("You see nothing!", "seeNon", "white");
	}
	
	this.act();
};

Player.prototype.attack = function(){
	var game = this.mapManager.game;
	var weapon = (game.player.weapon != null)? game.player.weapon.name : "Hands";
	
	var pos = this.position.clone();
	var dir = this.mapManager.getDirectionVector(this.direction);
	pos.sum(dir);
	
	var target = this.mapManager.getEnemyAt(pos);
	var tn = "";
	if (target != null) tn = target.name + " ";
	
	Console.addMessage("Attack " + tn + "using " + weapon , "attack", "aqua");
	
	if (target != null){
		this.mapManager.castAttack(game.player, target.character);
	}else{
		Console.addMessage("Nothing there!", "attack2", "white");
	}
	
	this.act();
};

Player.prototype.castMagic = function(){
	var game = this.mapManager.game;
	var spell = game.player.spell;
	
	if (spell != null){
		spell.effect.activate(this.position, this.direction);
	}else{
		Console.addMessage("No spell ready!", "spellNoR", "yellow");
	}
	
	this.act();
};

Player.prototype.step = function(){
	var game = this.mapManager.game;
	
	if (game.getKeyPressed(37)){
		if (++this.direction == 4) this.direction = 0;
		this.act();
	}else if (game.getKeyPressed(38)){
		this.moveFacing();
	}else if (game.getKeyPressed(39)){
		if (--this.direction < 0) this.direction = 3;
		this.act();
	}else if (game.getKeyPressed(40)){
		this.direction += 6;
		this.direction = this.direction % 4;
		this.act();
	}else if (game.getKeyPressed(76)){
		this.look();
	}else if (game.getKeyPressed(65)){
		this.attack();
	}else if (game.getKeyPressed(67)){
		this.castMagic();
	}
};

Player.prototype.loop = function(){
	this.action = false;
	
	this.step();
};
